hfrApp.registerCtrl("AddTopicBlogController", function ($scope, $http, $log, $timeout, $window, $translate, $rootScope, $timeout, $location, HfrConfig, HfrStorage, HfrInit) {
	//Init message alert
	HfrInit.message();
	
	//Display meta header
	HfrConfig.displayMeta("addTopicBlog");
	
	//Delay content for display after 0,8 second
	$timeout(function () {
		$scope.showViewContent = true;
	}, 800);
	
	$scope.processing = false;
	$scope.selectTopics = [];
	$scope.editForm = false;
	if ($location.path() == "/edit-topics") {
		$scope.editForm = true;
	}
	
	if ($rootScope.siteurl === undefined) {
		if (HfrStorage.get("siteUrl") !== null) {
			$rootScope.siteurl = HfrStorage.get("siteUrl");
		} else {
			$window.location.href= "#/add-new-site-wordpress";
		}
	}
	
	//Get all topic
	$scope.topics = {};
	$scope.autotopics = [];
	$http.get(HfrConfig.ApiServiceUrl.topicsUrlApi)
		.success(function (data) {
			if (data.status == 200) {
				$scope.topics = data.response.topics;
				angular.forEach(data.response.topics, function(topic, key) {
					$scope.autotopics.push(topic);
				});
			}
			
		})
		.error(function (data) {
			$rootScope.message.error = true;
			$rootScope.message.text = $translate.instant("message.error.connect");
		});
	
	//Get topic of blog
	$scope.showTopicSelected = false;
	var urlTopicOfBlog = HfrConfig.ApiServiceUrl.topicsUrlApi + "?blogId=" + $rootScope.siteurl; 
	$http.get(urlTopicOfBlog)
		.success(function (data) {
			if (data.status == 200) {
				angular.forEach(data.response.topics, function(topic, key) {
					$scope.selectTopics.push(topic.id);
				});
			} else {
				$scope.selectTopics = [];
				$rootScope.message.error = true;
				$rootScope.message.text = data.message;
			}
			$scope.showTopicSelected = true;
		})
		.error(function (data) {
			$scope.selectTopics = [];
			$scope.showTopicSelected = true;
			$rootScope.message.error = true;
			$rootScope.message.text = $translate.instant("message.error.connect");
		});
	
	$scope.filterAlreadyAdded = function(item) {
	    if( $.inArray(item.id, $scope.selectTopics)!=-1) {	
	    	return false;
	    }
	    return true;
	};
	/*
	 * ---------------------- Process Controller ---------------------------
	 */
	$scope.add_topic = function (topicForm) {
		$scope.data = {
			blogTopics:[]
		};
		
		angular.forEach($scope.selectTopics, function(value, key) {
			$scope.data.blogTopics.push({
				topicId: value,
				blogId: $rootScope.siteurl,
			});
		});
		
		if ($scope.sizeOf($scope.data.blogTopics) > 0) {
			$scope.processing = true;
			var blogTopicsUrl = HfrConfig.ApiServiceUrl.blogTopicsUrlApi;
			
			$http.post(blogTopicsUrl, $scope.data)
				.success(function (data) {
					if (data.status == 200) {
						$scope.success = true;
						$scope.processing = false;
						if ($scope.editForm) {
							$window.location.href= "#/site-management";
						} else {
							$window.location.href= "#/summary";
						}
					} else {
						$rootScope.message.error = true;
						$rootScope.message.text = data.message;
					}
				}).
			    error(function(data) {
			    	$rootScope.message.error = true;
					$rootScope.message.text = $translate.instant("message.error.connect");
			    });
		} else {
			$rootScope.message.error = true;
			$rootScope.message.text = $translate.instant("message.addTopicBlog.errorSelectTopic");
		}
		
	};
	
	$scope.select_topic = function (event) {
		if (event.which == 13) {
			var topicId = $scope.topicSelected.id;
			var idx = $scope.selectTopics.indexOf(topicId);
			if (idx <= -1 && $scope.topics.hasOwnProperty(topicId)) {
				$scope.selectTopics.push(topicId);
			}
			
			$scope.topicSelected = "";
		}
	};
	
	$scope.selectTypeaheadTopic = function ($item, $model, $label) {
		var topicId = $item.id;
		var idx = $scope.selectTopics.indexOf(topicId);
		if (idx <= -1 && $scope.topics.hasOwnProperty(topicId)) {
			$scope.selectTopics.push(topicId);
		}
		
		$scope.topicSelected = "";
	};
	
	$scope.remove_topic = function(topicId) {
		var idx = $scope.selectTopics.indexOf(topicId);
		if (idx > -1) {
			$scope.selectTopics.splice(idx, 1);
		}
	};
	
	//Count size
	$scope.sizeOf = function(obj) {
	    return Object.keys(obj).length;
	};
});